package org.gd.hellojavacore.tests;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Gorhover
 * @version 1.0-SNAPSHOT
 * @since 2013-11-08 11:16
 */
class ComparableTest {

    public static void main(String[] args) {
        List<In> inList = inList();
        Collections.sort(inList, In.COMPARATOR);
        for(In in: inList) {
            System.out.println(in.toString());
        }
    }

    private static List<In> inList() {
        List<In> inList = new ArrayList<>();
        inList.add(new Cl1());
        inList.add(new Cl1());
        inList.add(new Cl1());
        inList.add(new Cl1());
        inList.add(new Cl2());
        inList.add(new Cl2());
        inList.add(new Cl2());
        inList.add(new Cl2());
        inList.add(new Cl2());
        return inList;
    }

    private static interface In extends Comparable<In> {
        int getId();
        Comparator<In> COMPARATOR = new Comparator<In>() {
            @Override
            public int compare(In o1, In o2) {
                return o1.compareTo(o2);
            }
        };
    }

    private static abstract class Cl implements In {
        private int id;
        private Cl() {
            this.id = Double.valueOf(Math.random() * 1000).intValue();
        }
        @Override
        public int getId() {
            return id;
        }
        @Override
        public int compareTo(In o) {
            return getId() - o.getId();
        }
        @Override
        public String toString() {
            return String.format("%s { id=%d }",
                    getClass().getSimpleName(),
                    getId());
        }
    }

    private static class Cl1 extends Cl {}

    private static class Cl2 extends Cl {}
}
